home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / lmwrap / ogl / lmwrap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  17.2 KB  |  712 lines

  1. /*
  2.  * Copyright (c) 1995, Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that the name of Silicon Graphics may not be used in any advertising or
  7.  * publicity relating to the software without the specific, prior written
  8.  * permission of Silicon Graphics.
  9.  *
  10.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  11.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  12.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  13.  *
  14.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  15.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  17.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  21.  */
  22. /*----------------------------------------------------------------------------
  23.  *
  24.  * file   : lmwrap.c 
  25.  *
  26.  * Author : Yusuf Attarwala
  27.  * Date   : Apr 95
  28.  *
  29.  *          wrappers for irisGL style lmbind and lmdef calls
  30.  *          to be used in openGL applications
  31.  *
  32.  *---------------------------------------------------------------------------*/
  33. #include "lmwrap.h"
  34. #include "globals.h"
  35.  
  36. #define  UNDEFINED -99
  37.  
  38. /* max number of indices for each category */
  39. #define MAXLIGHT    16
  40. #define MAXLMODEL    6
  41. #define MAXMATERIAL 32
  42.  
  43. /* define some unique non zero number to initialize our tables */
  44. #define LMDEF_INIT  -765123
  45.  
  46. /* actual number of indices used by the application*/
  47. static int nLight, nLModel, nMaterial;
  48.  
  49. /* look up table between indices provided and indices used */
  50. static int lutLight[MAXLIGHT],
  51.        lutLModel[MAXLMODEL],
  52.        lutMaterial[MAXMATERIAL];
  53.  
  54. /* pointers to tables for storing lmdef info */
  55. static float *lightTable[MAXLIGHT], 
  56.          *lModelTable[MAXLMODEL], 
  57.          *materialTable[MAXMATERIAL];
  58.  
  59. /* max number of entries in each table */
  60. #define NLIGHTENTRY 17
  61. #define NMATERIALENTRY 21
  62. #define NLMODELENTRY 9
  63.  
  64. /* these are the offsets of specific values in tables */
  65.  
  66. #define MATAMBIENT      0     /* 4 floats */
  67. #define MATDIFFUSE      4     /* 4 floats */
  68. #define MATALPHA        7     /* 1 float  */  /* 4th component of diffuse */
  69. #define MATSPECULAR     8     /* 4 floats */
  70. #define MATEMISSION     12    /* 4 floats */
  71. #define MATSHININESS    16    /* 1 float  */
  72. #define MATCOLORINDEXES 17    /* 3 float  */
  73.  
  74. #define LITAMBIENT        0     /* 4 floats */
  75. #define LITLCOLOR         4     /* 4 floats */
  76. #define LITPOSITION       8     /* 4 floats */
  77. #define LITSPOTDIRECTION  12    /* 3 floats */
  78. #define LITSPOTLIGHT      15    /* 2 floats */
  79.  
  80. #define LTMAMBIENT        0     /* 4 floats */
  81. #define LTMLOCALVIEWER    4     /* 1 float  */
  82. #define LTMTWOSIDE        5     /* 1 float  */
  83. #define LTMATTENUATION    6     /* 2 floats */
  84. #define LTMATTENUATION2   8     /* 1 floats */
  85.  
  86. /* base of display lists */
  87. static GLuint baseLightDL, baseLModelDL, baseMaterialDL;
  88.  
  89. static int boundLModel;
  90.  
  91. void
  92. lmdef(deftype, index, np, props)
  93. short deftype, index, np;
  94. float props[];
  95. {
  96.     static int firstTime = 1;
  97.     int i;
  98.     int isDefined;
  99.     GLuint dList;
  100.     float *table,*tbl,*prp;
  101.  
  102.     GLenum face = GL_FRONT; 
  103.  
  104.     if (firstTime) {
  105.     firstTime = 0;
  106.  
  107.     baseLightDL    = glGenLists(MAXLIGHT);
  108.     baseLModelDL   = glGenLists(MAXLMODEL);
  109.     baseMaterialDL = glGenLists(MAXMATERIAL);
  110.  
  111.     nLight = nLModel = nMaterial = 0;
  112.     }
  113.  
  114.     /* for each deftype do the following :
  115.        
  116.        check if lmdef has been called before with the same index.
  117.        if so, blow away the appropriate display list and create new one.
  118.        if not, create a new DL and update, the lut etc
  119.  
  120.      */
  121.  
  122.     switch(deftype) {
  123.     case DEFLIGHT    :
  124.     isDefined = UNDEFINED;
  125.  
  126.     for (i=0;i<MAXLIGHT;i++) {
  127.         if (index == lutLight[i]) {
  128.         isDefined = i;
  129.         break;
  130.         }
  131.     }
  132.  
  133.     if (isDefined == UNDEFINED) {
  134.         if (nLModel >= MAXLIGHT) {
  135.         printf("ran out of light tables \n");
  136.         return;
  137.         }
  138.  
  139.         lutLight[nLight] = index;
  140.         lightTable[nLight] = (float *) malloc(sizeof(float) * NLIGHTENTRY);
  141.         table = lightTable[nLight];
  142.  
  143.         /* insert some non zero unique value to initialize */
  144.         for (i=0;i<NLIGHTENTRY;i++) {
  145.         *(table+i) = LMDEF_INIT;
  146.             }
  147.         table = lightTable[nLight];
  148.         nLight++;
  149.     }
  150.     else {
  151.         table = lightTable[isDefined];
  152.     }
  153.  
  154.     if (props == NULL) {
  155.  
  156.         /* just create table with defaults */
  157.  
  158.         /* purge previous definitions */
  159.  
  160.         for (i=0;i<NLIGHTENTRY;i++) {
  161.         *(table+i) = LMDEF_INIT;
  162.             }
  163.  
  164.         /* these are the light model AMBIENT parameters */
  165.  
  166.         tbl = table + LITAMBIENT;
  167.  
  168.         *tbl++ = 0.2;
  169.         *tbl++ = 0.2;
  170.         *tbl++ = 0.2;
  171.         *tbl++ = 1.0;
  172.     }
  173.     else {
  174.         /* create/update tables with new values */
  175.  
  176.         prp = (float *)&props[0];
  177.  
  178.         while (*prp != LMNULL) {
  179.         if (*prp == AMBIENT) {
  180.             prp++;
  181.                 tbl = table + LITAMBIENT;
  182.             for (i=0;i<3;i++)*tbl++ = *prp++;
  183.             *tbl++ = 1.0;
  184.         }
  185.         else if (*prp == LCOLOR) {
  186.             prp++;
  187.                 tbl = table + LITLCOLOR;
  188.             for (i=0;i<3;i++)*tbl++ = *prp++;
  189.             *tbl++ = 1.0;
  190.         }
  191.         else if (*prp == POSITION) {
  192.             prp++;
  193.                 tbl = table + LITPOSITION;
  194.             for (i=0;i<4;i++)*tbl++ = *prp++;
  195.         }
  196.         else if (*prp == SPOTDIRECTION) {
  197.             prp++;
  198.                 tbl = table + LITSPOTDIRECTION;
  199.             for (i=0;i<3;i++)*tbl++ = *prp++;
  200.         }
  201.         else if (*prp == SPOTLIGHT) {
  202.             prp++;
  203.                 tbl = table + LITSPOTLIGHT;
  204.             for (i=0;i<2;i++)*tbl++ = *prp++;
  205.         }
  206.         else {
  207.             printf("invalid field [%d] in lmdef LIGHT \n",*prp++);
  208.         }
  209.         } /* while */
  210.     }
  211.  
  212.     /*
  213.  
  214.            Reason  for not creating display list for light tables :
  215.  
  216.        no display list is created here, because in lmdef of light,
  217.        we don't know which light will be bound with index
  218.        e.g GL_LIGHT0 or GL_LIGHT1 or...
  219.        the display list can be only created if we have that
  220.        info, because glLightfv(...) needs it,
  221.        therefore, just store the values in our table and
  222.        use them in lmbind in immediate mode
  223.  
  224.          */
  225.     break;
  226.  
  227.     case DEFLMODEL   :
  228.     isDefined = UNDEFINED;
  229.  
  230.     for (i=0;i<MAXLMODEL;i++) {
  231.         if (index == lutLModel[i]) {
  232.         isDefined = i;
  233.         break;
  234.         }
  235.     }
  236.  
  237.     if (isDefined == UNDEFINED) {
  238.         if (nLModel >= MAXLMODEL) {
  239.         printf("ran out of light model tables \n");
  240.         return;
  241.         }
  242.  
  243.         dList = baseLModelDL + nLModel;
  244.         lutLModel[nLModel] = index;
  245.         lModelTable[nLModel] = (float *)
  246.                     malloc(sizeof(float) * NLMODELENTRY);
  247.  
  248.  
  249.         table = lModelTable[nLModel];
  250.  
  251.         /* insert some non zero unique value to initialize */
  252.         for (i=0;i<NLMODELENTRY;i++) {
  253.         *(table+i) = LMDEF_INIT;
  254.             }
  255.  
  256.         table = lModelTable[nLModel];
  257.  
  258.         nLModel++;
  259.     }
  260.     else {
  261.         
  262.         dList = baseLModelDL + isDefined;
  263.         glDeleteLists(dList,1);
  264.         table = lModelTable[isDefined];
  265.  
  266.     }
  267.  
  268.     if (props == NULL) {
  269.  
  270.         /* just create table with defaults */
  271.  
  272.         /* purge previous definitions */
  273.  
  274.         for (i=0;i<NLMODELENTRY;i++) {
  275.         *(table+i) = LMDEF_INIT;
  276.             }
  277.  
  278.         /* these are the light model AMBIENT parameters */
  279.  
  280.         tbl = table + LTMAMBIENT;
  281.  
  282.         *tbl++ = 0.2;
  283.         *tbl++ = 0.2;
  284.         *tbl++ = 0.2;
  285.         *tbl++ = 1.0;
  286.     }
  287.     else {
  288.         /* create/update tables with new values */
  289.  
  290.         prp = (float *)&props[0];
  291.  
  292.         while (*prp != LMNULL) {
  293.         if (*prp == AMBIENT) {
  294.             prp++;
  295.                 tbl = table + LTMAMBIENT;
  296.             for (i=0;i<3;i++)*tbl++ = *prp++;
  297.             *tbl++ = 1.0;
  298.         }
  299.         else if (*prp == LOCALVIEWER) {
  300.             prp++;
  301.                 tbl = table + LTMLOCALVIEWER;
  302.             *tbl = *prp++;
  303.         }
  304.         else if (*prp == TWOSIDE) {
  305.             prp++;
  306.                 tbl = table + LTMTWOSIDE;
  307.             *tbl = *prp++;
  308.         }
  309.         else if (*prp == ATTENUATION) {
  310.             prp++;
  311.                 tbl = table + LTMATTENUATION;
  312.             for (i=0;i<2;i++)*tbl++ = *prp++;
  313.         }
  314.         else if (*prp == ATTENUATION2) {
  315.             prp++;
  316.                 tbl = table + LTMATTENUATION2;
  317.             *tbl = *prp++;
  318.         }
  319.         else {
  320.             printf("invalid field [%d] in lmdef LMODEL \n",*prp++);
  321.         }
  322.         } /* while */
  323.     }
  324.  
  325.     /* generate the list from our tables */
  326.     glNewList(dList,GL_COMPILE); 
  327.  
  328.     if (*(table + LTMAMBIENT) != LMDEF_INIT) {
  329.         glLightModelfv(GL_LIGHT_MODEL_AMBIENT,(table + LTMAMBIENT));
  330.     }
  331.     if (*(table + LTMLOCALVIEWER) != LMDEF_INIT) {
  332.         glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER,*(table+LTMLOCALVIEWER));
  333.     }
  334.     if (*(table + LTMTWOSIDE) != LMDEF_INIT) {
  335.         glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,*(table + LTMTWOSIDE));
  336.     }
  337.     /* ATTENUATIONS are taken care in lmbind of lights */
  338.  
  339.     glEndList();
  340.     break;
  341.     case DEFMATERIAL :
  342.     isDefined = UNDEFINED;
  343.  
  344.     for (i=0;i<MAXMATERIAL;i++) {
  345.         if (index == lutMaterial[i]) {
  346.         isDefined = i;
  347.         break;
  348.         }
  349.     }
  350.  
  351.     if (isDefined == UNDEFINED) {
  352.         if (nMaterial >= MAXMATERIAL) {
  353.         printf("ran out of mateial tables \n");
  354.         return;
  355.         }
  356.  
  357.         dList = baseMaterialDL + nMaterial;
  358.         lutMaterial[nMaterial] = index;
  359.         materialTable[nMaterial] = (float *)
  360.                     malloc(sizeof(float) * NMATERIALENTRY);
  361.  
  362.  
  363.         table = materialTable[nMaterial];
  364.  
  365.         /* insert some non zero unique value to initialize */
  366.         for (i=0;i<NMATERIALENTRY;i++) {
  367.         *(table+i) = LMDEF_INIT;
  368.             }
  369.  
  370.         nMaterial++;
  371.     }
  372.     else {
  373.         
  374.         dList = baseMaterialDL + isDefined;
  375.         glDeleteLists(dList,1);
  376.         table = materialTable[isDefined];
  377.  
  378.     }
  379.  
  380.     if (props == NULL) {
  381.  
  382.         /* just create table with defaults */
  383.  
  384.         /* purge previous definitions */
  385.  
  386.         for (i=0;i<NMATERIALENTRY;i++) {
  387.         *(table+i) = LMDEF_INIT;
  388.             }
  389.  
  390.         /* these are the AMBIENT parameters */
  391.  
  392.         tbl = table + MATAMBIENT;
  393.  
  394.         *tbl++ = 0.2;
  395.         *tbl++ = 0.2;
  396.         *tbl++ = 0.2;
  397.         *tbl++ = 1.0;
  398.     }
  399.     else {
  400.         /* create/update tables with new values */
  401.  
  402.         prp = (float *)&props[0];
  403.  
  404.         while (*prp != LMNULL) {
  405.         if (*prp == ALPHA) {
  406.             prp++;   /* can't increment in if */
  407.                 tbl = table + MATALPHA;
  408.             *tbl = *prp++;
  409.         }
  410.         else if (*prp == AMBIENT) {
  411.             prp++;
  412.                 tbl = table + MATAMBIENT;
  413.             for (i=0;i<3;i++)*tbl++ = *prp++;
  414.             *tbl++ = 1.0;
  415.         }
  416.         else if (*prp == DIFFUSE) {
  417.             prp++;
  418.                 tbl = table + MATDIFFUSE;
  419.             for (i=0;i<3;i++)*tbl++ = *prp++;
  420.             if (*tbl != LMDEF_INIT) *tbl++ = 1.0;
  421.         }
  422.         else if (*prp == SPECULAR) {
  423.             prp++;
  424.                 tbl = table + MATSPECULAR;
  425.             for (i=0;i<3;i++)*tbl++ = *prp++;
  426.             *tbl++ = 1.0;
  427.         }
  428.         else if (*prp == EMISSION) {
  429.             prp++;
  430.                 tbl = table + MATEMISSION;
  431.             for (i=0;i<3;i++)*tbl++ = *prp++;
  432.             *tbl++ = 1.0;
  433.         }
  434.         else if (*prp == SHININESS) {
  435.             prp++;
  436.                 tbl = table + MATSHININESS;
  437.             *tbl = *prp++;
  438.         }
  439.         else if (*prp == COLORINDEXES) {
  440.             prp++;
  441.                 tbl = table + MATCOLORINDEXES;
  442.             for (i=0;i<3;i++)*tbl++ = *prp++;
  443.         }
  444.         else {
  445.             printf("invalid field [%d] in lmdef MATERIAL \n",*prp++);
  446.         }
  447.         } /* while */
  448.     }
  449.  
  450.     /* generate the list from our tables */
  451.     glNewList(dList,GL_COMPILE); 
  452.  
  453.     if (*(table + MATAMBIENT) != LMDEF_INIT) {
  454.         glMaterialfv(face,GL_AMBIENT,(table + MATAMBIENT));
  455.     }
  456.     if (*(table + MATDIFFUSE) != LMDEF_INIT) {
  457.         glMaterialfv(face,GL_DIFFUSE,(table + MATDIFFUSE));
  458.     }
  459.     if (*(table + MATSPECULAR) != LMDEF_INIT) {
  460.         /* in irisGL if the shininess is zero or undefined,
  461.            then even specular is not added */
  462.         if (*(table + MATSHININESS) != 0.0 &&
  463.         *(table + MATSHININESS) != LMDEF_INIT) {
  464.             glMaterialfv(face,GL_SPECULAR,(table + MATSPECULAR));
  465.         }
  466.     }
  467.     if (*(table + MATEMISSION) != LMDEF_INIT) {
  468.         glMaterialfv(face,GL_EMISSION,(table + MATEMISSION));
  469.     }
  470.     if (*(table + MATSHININESS) != LMDEF_INIT) {
  471.         glMaterialf(face,GL_SHININESS,*(table + MATSHININESS));
  472.     }
  473.     if (*(table + MATCOLORINDEXES) != LMDEF_INIT) {
  474.         glMaterialfv(face,GL_COLOR_INDEXES,(table + MATCOLORINDEXES));
  475.     }
  476.  
  477.     glEndList();
  478.  
  479.     break;
  480.     default :
  481.     printf("invalid type for lmdef [%d] \n",deftype);
  482.     return;
  483.     }
  484. }
  485.  
  486. void
  487. lmbind(target,index)
  488. short target,index;
  489. {
  490.     int i;
  491.     GLuint dList;
  492.     int    lightOffset;
  493.     GLboolean lightOn;
  494.     float *table,*tbl,*prp;
  495.  
  496.     GLenum face = GL_BACK; 
  497.  
  498.     switch(target) {
  499.     case LIGHT0 :
  500.     case LIGHT1 :
  501.     case LIGHT2 :
  502.     case LIGHT3 :
  503.     case LIGHT4 :
  504.     case LIGHT5 :
  505.     case LIGHT6 :
  506.     case LIGHT7 :
  507.  
  508.     lightOffset = GL_LIGHT0 + ( target - LIGHT0) ;
  509.     if (index == 0) {
  510.         glDisable(lightOffset);
  511.  
  512.         /* if all the lights are off, then disable lighting */
  513.         lightOn = False;
  514.         for (i=0;i<8;i++) {
  515.         if (glIsEnabled(GL_LIGHT0 + i)) {
  516.             lightOn = True;
  517.             break;
  518.         }
  519.         }
  520.         if (!lightOn) glDisable(GL_LIGHTING);
  521.     }
  522.     else {
  523.  
  524.         dList = UNDEFINED;
  525.         for (i=0;i<nLight;i++) {
  526.         if (lutLight[i] == index) {
  527.             dList = i;
  528.  
  529.             break;
  530.         }
  531.         }
  532.  
  533.         if (dList == UNDEFINED) {
  534.         printf("lmbind-LIGHT, index [%d] not defined\n",index);
  535.         return;
  536.         }
  537.         else {
  538.         /* we don't have a display list here, see reason
  539.            in lmdef light */
  540.  
  541.                 table = lightTable[dList];
  542.  
  543.         if (*(table + LITAMBIENT) != LMDEF_INIT) {
  544.                 glLightfv(lightOffset,GL_AMBIENT,(table + LITAMBIENT));
  545.             }
  546.         if (*(table + LITLCOLOR) != LMDEF_INIT) {
  547.                 glLightfv(lightOffset,GL_DIFFUSE,(table + LITLCOLOR));
  548.             }
  549.         if (*(table + LITPOSITION) != LMDEF_INIT) {
  550.                 glLightfv(lightOffset,GL_POSITION,(table + LITPOSITION));
  551.             }
  552.         if (*(table + LITSPOTDIRECTION) != LMDEF_INIT) {
  553.                 glLightfv(lightOffset,GL_SPOT_DIRECTION,
  554.                       (table+LITSPOTDIRECTION));
  555.             }
  556.         if (*(table + LITSPOTLIGHT) != LMDEF_INIT) {
  557.                 glLightf(lightOffset,GL_SPOT_EXPONENT,
  558.                      *(table+LITSPOTLIGHT));
  559.                 glLightf(lightOffset,GL_SPOT_CUTOFF,
  560.                      *(table+LITSPOTLIGHT+1));
  561.             }
  562.  
  563.         /* see if the current light model has attentuation,
  564.            if so, apply it to the light 
  565.  
  566.            boundLModel points to the table of currently bound
  567.            lighting model
  568.  
  569.         */
  570.  
  571.         if (boundLModel != UNDEFINED) {
  572.                     table = lModelTable[boundLModel];
  573.  
  574.             if (*(table + LTMATTENUATION) != LMDEF_INIT) {
  575.             glLightf(lightOffset,GL_CONSTANT_ATTENUATION,
  576.                 *(table+LTMATTENUATION));
  577.             glLightf(lightOffset,GL_LINEAR_ATTENUATION,
  578.                 *(table+LTMATTENUATION+1));
  579.             }
  580.             if (*(table + LTMATTENUATION2) != LMDEF_INIT) {
  581.             glLightf(lightOffset,GL_QUADRATIC_ATTENUATION,
  582.                 *(table+LTMATTENUATION2));
  583.             }
  584.         }
  585.         }
  586.  
  587.         if (!glIsEnabled(GL_LIGHTING)) glEnable(GL_LIGHTING);
  588.     }
  589.     break;
  590.     case LMODEL :
  591.     if (index == 0) {
  592.         /* in irisGL, turning off a material or light model, 
  593.            turns lighting off */
  594.         if (glIsEnabled(GL_LIGHTING)) glDisable(GL_LIGHTING);
  595.     }
  596.     else {
  597.         /* from the lut, find the display list */
  598.  
  599.         boundLModel = dList = UNDEFINED;
  600.         for (i=0;i<nLModel;i++) {
  601.         if (lutLModel[i] == index) {
  602.             dList = baseLModelDL + i;
  603.             boundLModel = i;
  604.             break;
  605.         }
  606.         }
  607.  
  608.         if (dList == UNDEFINED) {
  609.         printf("lmbind-LMODEL, index [%d] not defined\n",index);
  610.         return;
  611.         }
  612.         else {
  613.         glCallList(dList);   
  614.             if (!glIsEnabled(GL_LIGHTING)) glEnable(GL_LIGHTING);
  615.         }
  616.     }
  617.     break;
  618.     case MATERIAL :
  619.     if (index == 0) {
  620.         /* in irisGL, turning off a material or light model, 
  621.            turns lighting off */
  622.         if (glIsEnabled(GL_LIGHTING)) glDisable(GL_LIGHTING);
  623.     }
  624.     else {
  625.         /* from the lut, find the display list */
  626.  
  627.         dList = UNDEFINED;
  628.         for (i=0;i<nMaterial;i++) {
  629.         if (lutMaterial[i] == index) {
  630.             dList = baseMaterialDL + i;
  631.             break;
  632.         }
  633.         }
  634. /*
  635. printf("FRONTMAT %d \n",dList-baseMaterialDL);
  636. */
  637.  
  638.         if (dList == UNDEFINED) {
  639.         printf("lmbind-MATERIAL, index [%d] not defined\n",index);
  640.         return;
  641.         }
  642.         else {
  643.         glCallList(dList);   
  644.             if (!glIsEnabled(GL_LIGHTING)) glEnable(GL_LIGHTING);
  645.         }
  646.     }
  647.     break;
  648.     case BACKMATERIAL :
  649.  
  650.     /* display list has been created with GL_FRONT,
  651.        assuming BACKMATERIAL is not used frequently,
  652.        use the values from tables in immediate mode 
  653.        instead of creating storing display list */
  654.  
  655.     if (index != 0) {
  656.         /* index = 0 has no effect on lighting ON,OFF so
  657.            ignore it */
  658.  
  659.         /* from the lut, find the display list */
  660.  
  661.         dList = UNDEFINED;
  662.         for (i=0;i<nMaterial;i++) {
  663.         if (lutMaterial[i] == index) {
  664.             dList = i;
  665.             break;
  666.         }
  667.         }
  668.  
  669.         if (dList == UNDEFINED) {
  670.         printf("lmbind-BACKMATERIAL, index [%d] not defined\n",index);
  671.         return;
  672.         }
  673.         else {
  674.             if (!glIsEnabled(GL_LIGHTING)) glEnable(GL_LIGHTING);
  675.         }
  676.  
  677.         table = materialTable[dList];
  678. /*
  679. printf("BACKMAT %d \n",dList);
  680. */
  681.  
  682.         if (*(table + MATAMBIENT) != LMDEF_INIT) {
  683.         glMaterialfv(face,GL_AMBIENT,(table + MATAMBIENT));
  684.         }
  685.         if (*(table + MATDIFFUSE) != LMDEF_INIT) {
  686.         glMaterialfv(face,GL_DIFFUSE,(table + MATDIFFUSE));
  687.         }
  688.         if (*(table + MATSPECULAR) != LMDEF_INIT) {
  689.         /* in irisGL if the shininess is zero or undefined,
  690.            then even specular is not added */
  691.         if (*(table + MATSHININESS) != 0.0 &&
  692.             *(table + MATSHININESS) != LMDEF_INIT) {
  693.             glMaterialfv(face,GL_SPECULAR,(table + MATSPECULAR));
  694.         }
  695.         }
  696.         if (*(table + MATEMISSION) != LMDEF_INIT) {
  697.         glMaterialfv(face,GL_EMISSION,(table + MATEMISSION));
  698.         }
  699.         if (*(table + MATSHININESS) != LMDEF_INIT) {
  700.         glMaterialf(face,GL_SHININESS,*(table + MATSHININESS));
  701.         }
  702.         if (*(table + MATCOLORINDEXES) != LMDEF_INIT) {
  703.         glMaterialfv(face,GL_COLOR_INDEXES,(table + MATCOLORINDEXES));
  704.         }
  705.     }
  706.     break;
  707.     default :
  708.     printf("invalid target for lmbind [%d] \n",target);
  709.     return;
  710.     }
  711. }
  712.